package com.example.growthmuseum.controllers;

import com.example.growthmuseum.daos.IGoodsDao;
import com.example.growthmuseum.models.Goods;
import com.example.growthmuseum.models.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * CompetitionController.
 * <p>
 * Created by liukaixin on 16/8/20.
 */
@RestController
@RequestMapping("/growth-museum/goods")
public class GoodsController {

    private static final Logger LOGGER =
            LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    private IGoodsDao goodsDao;

    /**
     * 添加物品.
     */
    @RequestMapping(value = "/addGoods", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result<Integer>>
    addCompetition(@RequestBody Goods goods) {
        LOGGER.info("receive request>>addGoods:[{}] ", goods.toString());
        Result<Integer> result = new Result<>();
        result.setCode(Result.SUCCESS);
        try {
            goods.setGoodsPic("https://gd1.alicdn.com/bao/uploaded/i1/523635203/TB2HhDLgpXXXXb2XXXXXXXXXXXX_!!523635203.jpg_600x600.jpg");
            result.setBean(goodsDao.addGoods(goods));
            result.setMessage("添加物品成功");
        } catch (RuntimeException e) {
            result.setCode(Result.FAILED);
            result.setMessage(e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 查询某个人物品.
     */
    @RequestMapping(value = "/getGoodsListByUserId/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Result<List<Goods>>>
    getGoodsListByUserId(@PathVariable Integer userId) {
        LOGGER.info("receive request>>getGoodsListByUserId:[{}] ", userId);
        Result<List<Goods>> result = new Result<>();
        result.setCode(Result.SUCCESS);
        try {
            result.setBean(goodsDao.getGoodsListByUserId(userId));
            result.setMessage("获取某用户物品成功");
        } catch (RuntimeException e) {
            result.setCode(Result.FAILED);
            result.setMessage(e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 查询物品列表.
     */
    @RequestMapping(value = "/getGoodsList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Result<List<Goods>>>
    getGoodsList() {
        LOGGER.info("receive request>>getGoodsList");
        Result<List<Goods>> result = new Result<>();
        result.setCode(Result.SUCCESS);
        try {
            result.setBean(goodsDao.getGoodsList());
            result.setMessage("获取所有物品成功");
        } catch (RuntimeException e) {
            result.setCode(Result.FAILED);
            result.setMessage(e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 查询物品详情.
     */
    @RequestMapping(value = "/getGoodsById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Result<Goods>>
    getGoodsById(@PathVariable Integer id) {
        LOGGER.info("receive request>>getGoodsById,[{}]", id);
        Result<Goods> result = new Result<>();
        result.setCode(Result.SUCCESS);
        try {
            result.setBean(goodsDao.getGoodsById(id));
            result.setMessage("获取物品详情成功");
        } catch (RuntimeException e) {
            result.setCode(Result.FAILED);
            result.setMessage(e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 更改物品.
     */
    @RequestMapping(value = "/updateGoods", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity<Result<Integer>>
    updateGoods(@RequestBody Goods goods) {
        LOGGER.info("receive request>>updateGoods,goods = [{}]", goods.toString());
        Result<Integer> result = new Result<>();
        result.setCode(Result.SUCCESS);
        try {
            result.setBean(goodsDao.updateGoods(goods));
            result.setMessage("更改物品成功");
        } catch (RuntimeException e) {
            result.setCode(Result.FAILED);
            result.setMessage(e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

}
